#ifndef __PAINTER__
#define __PAINTER__

#include <windows.h>
#include "base/wstring.h"
#include "base/array.h"
#include "frameui/image.h"
#include "shared.h"

#include "irc.h"

struct OverlayParams
{
  wchar_t nick[256];
  wchar_t pass[256];
  wchar_t chan[256];

  double left;
  double top;
  double right;
  double bottom;
  bool lockPosition;
  uint32 toggleHotkey;
  uint32 fgAlpha; // = 255
  uint32 background; // input
  uint32 borderColor; // = textColor
  uint32 borderWidth; // = 2
  uint32 padding; // = 8
  uint32 lineHeight; // = 20
  wchar_t fontFace[256]; // input
  uint32 fontSize; // input
  uint32 textColor; // input
  uint32 sysColor; // (nameColorMin+nameColorMax)/2
  uint32 nameColorMin; // textColor*(1-bgalpha)+background*bgalpha
  uint32 nameColorMax; // textColor
  uint32 flags; // 0

  uint32 port;
  uint32 replyHotkey;
};
struct ChatLine
{
  enum {tMessage, tEmote, tSystem,
        tDeleted = 0x80};
  uint32 type;
  WideString name;
  WideString text;
};

#define CHAT_CLEAR            0
#define CHAT_CONNECTED        1
#define CHAT_MESSAGE          2
#define CHAT_SYSTEM           3
#define CHAT_CLEARUSER        4

class TwitchClient;
class MainWnd;

class OverlayPainter
{
  SharedData data;
  HWND hWndPrev;

  MainWnd* mainWnd;

  bool repaintPreview;
  Image preview;

  struct RenderBlock
  {
    Array<Image> lines;
    int lineWidth;
    int lineHeight;
    int textSize;
    int curWidth;
    RenderBlock(int w, int h, int t);
  };

  Image* renderText(HFONT hFont, wchar_t const* text, int maxWidth, bool isfirst, int* end);
  void lineAddImage(Image* dst, int y, int& x, Image const* src, uint32 color);
  int lineAddText(Image* dst, int y, int& x, int maxWidth, wchar_t const* text, HFONT hFont, uint32 color);
  void blockStart(RenderBlock* blk);
  void blockAddImage(RenderBlock* blk, Image const* src, int pad = 0);
  void blockAddText(RenderBlock* blk, wchar_t const* text, HFONT hFont, uint32 color);
  void blockAddTextEmotes(RenderBlock* blk, wchar_t const* text, Array<int> const* emoteset, HFONT hFont, uint32 color);
  void blockNext(RenderBlock* blk);

  void drawFrame();
  void drawScroll();
  void drawLines();
  void drawPreview();
  void doDrawPreview();

  TwitchClient* client;
  uint32 titleHash;

  OverlayParams params;
  Array<ChatLine> lines;
public:
  OverlayPainter();
  ~OverlayPainter();

  void setClient(TwitchClient* c)
  {
    client = c;
  }
  
  void addMessage(int type, char const* name, char const* text);

  Image& getPreview()
  {
    if (repaintPreview)
      doDrawPreview();
    return preview;
  }

  OverlayParams& getParams()
  {
    return params;
  }
  void updateParams();

  void resetPos();
  void repaint();

  void update();

  HWND getTargetWnd();
  bool getTargetRect(RECT* rc);
  bool setTargetRect(RECT const* rc);
  void updateRect();

  bool getScrollRect(RECT* rc);
  bool setScrollTop(int y);

  bool lock();
  void unlock();

  int hidden();
  void setHidden(int h);

  void setMainWnd(MainWnd* wnd);

  SharedData* getData()
  {
    return &data;
  }

  void updateReply();
  void postReply();
};

#endif // __PAINTER__
